<template>
  <div class="login-page">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span class="login-title">智农视窗</span>
      </div>
      <div class="login-form">
        <el-form :model="form" :rules="loginRules" ref="loginForm">
          <el-form-item prop="userName">
            <el-input
                type="text"
                v-model="form.userName"
                auto-complete="off"
                placeholder="请输入用户名"
            >
              <template slot="prepend">
                <i style="font-size: 20px" class="el-icon-user"></i>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="passWord">
            <el-input
                type="password"
                v-model="form.passWord"
                auto-complete="off"
                placeholder="请输入密码"
            >
              <template slot="prepend">
                <i style="font-size: 20px" class="el-icon-key"></i>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button
                style="width: 100%"
                type="primary"
                @click="handleLogin"
                :loading="loading"
            >登录</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "LoginPage",
  data() {
    return {
      loading: false, // 加载状态
      form: {
        userName: "",
        passWord: "",
      },
      loginRules: {
        userName: [
          { required: true, message: "请输入账户", trigger: "blur" },
        ],
        passWord: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    async handleLogin() {
      try {
        // 表单验证
        const valid = await this.$refs.loginForm.validate();
        if (!valid) return;

        this.loading = true;

        // 使用 URLSearchParams 构建表单数据
        const params = new URLSearchParams();
        params.append("username", this.form.userName);
        params.append("password", this.form.passWord);

        // 发送登录请求，设置请求头为表单格式
        const response = await axios.post("/user/login", params, {
          headers: {
            "Content-Type": "application/x-www-form-urlencoded",
          },
        });

        if (response.status === 200) {
          await this.$router.push("/index");
          localStorage.setItem("authToken", response.data.token);
        } else {
          this.$message.error("登录失败，请重试");
        }
      } catch (error) {
        if (error.response) {
          this.$message.error(error.response.data.message || "登录失败");
        } else {
          this.$message.error("网络错误，请检查连接");
        }
        console.error("登录错误:", error);
      } finally {
        this.loading = false;
      }
    },
  },
};
</script>

<style scoped>
.login-page {
  background-image: linear-gradient(180deg, #2af598 0%, #009efd 100%);
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-title {
  font-size: 20px;
}

.box-card {
  width: 375px;
}
</style>